﻿using static _01_Tower4._0.GameFrame.System.ToolMenu;
using _01_Tower4._0.CharacterBase;
using _01_Tower4._0.GameFrame;
using _01_Tower4._0.Interface;
using _01_Tower4._0.GameFrame.Manager;
using _01_Tower4._0.GameFrame.System;
using static _01_Tower4._0.GameFrame.Game;
using System.Numerics;
using _01_Tower4._0.CharacterBase.EnemyType;
using _01_Tower4._0.Tool;

namespace _01_Tower4._0.Scene
{
    internal class SceneMap1 : IScene
    {
        Map map;
        CombatSystem cs;
        Game game;
        public Hero hero;
        MaO mao;
        DialogSystem ds;

        public List<GameObject> slime;
        public List<GameObject> goblin;
        public List<GameObject> fKing;
        public List<GameObject> rKing;
        public List<GameObject> npc1;
        public List<GameObject> npc2;
        public List<GameObject> pastor;
        public List<GameObject> maO;



        int dia;
        public static string? csTarget;
        public static string? dsTarget;
        public static bool isBoss;

        public string Name => "SceneMap1";

        public SceneMap1(CombatSystem cs, Map map, DialogSystem ds,Hero hero)
        {
            this.cs = cs;
            this.map = map;
            this.ds = ds;
            //this.hero = hero;
        }

        public void Init()
        {
            SceneManager.sceneName = "SceneMap1";

            string[] paths = { "../../../Map/map.csv",
                               "../../../Map/map2.csv",
                               "../../../Map/map3.csv", 
                               "../../../Map/map1.2.csv"};
            map = new Map(paths, 60, 30, 0, 0, this);
            // todo 学过数据结构之后优化 注册
            cs = new CombatSystem(62,22,this);
            ds = new DialogSystem(this);
            
            mao = new MaO("魔王", 300, 300, 150, 150, 30, 30, new Pos(), map, "魔", ConsoleColor.Red, this);
            hero = new Hero("玩家", 100, 100, 100, 100, 26, 20, new Pos(), map, "玩", ConsoleColor.DarkYellow, this,mao);

            slime = new List<GameObject>();
            goblin = new List<GameObject>();
            fKing = new List<GameObject>();
            rKing = new List<GameObject>();
            npc1 = new List<GameObject>();
            npc2 = new List<GameObject>();
            pastor = new List<GameObject>();
            maO = new List<GameObject>();

            map.ChangeMap(0);
            Map.pasterDialogId = 240;
            Map.rKingDialogId = 10;
            Map.npc1DialogId = 200;
            Map.npc2DialogId = 220;

            hero.Init(map, cs, ds);
            hero.AddInteractionAction(PlayerInteracion);
            csTarget = " ";
            ds.overDialog += () => Character.IsDialoging = false;
            isBoss = false;
        }

        public void Input()
        {
            hero.Input();
            if (SceneManager.sceneName == "SceneMap1")// 主场景
            {
                if (InputSystem.Instance.IsKeyPressed(ConsoleKey.O))// 主场景切背包
                {
                    InventorySystem.IsOpen = !InventorySystem.IsOpen;
                    SceneManager.lastSceneName = "SceneMap1";
                    SceneManager.sceneName = "SceneBag";
                }
                if (InputSystem.Instance.IsKeyPressed(ConsoleKey.I))// 主场景切技能列表
                {
                    SkillSystem.IsOpen = !SkillSystem.IsOpen;
                    SceneManager.lastSceneName = "SceneMap1";
                    SceneManager.sceneName = "SceneSkill";
                }
                if (InputSystem.Instance.IsKeyPressed(ConsoleKey.Escape))// 菜单
                {
                    EventSystem.Instance.InvokeEvent("更改选项名称");
                    SceneManager.instance.ChangeSceneByName("SceneMenu");
                }
                return;
            }
            if (SceneManager.sceneName == "SceneBag")// 背包
            {
                if(Character.IsCombating)return;
                if (InputSystem.Instance.IsKeyPressed(ConsoleKey.O))// 背包切回主场景
                {
                    InventorySystem.IsOpen = !InventorySystem.IsOpen;
                    SceneManager.sceneName = "SceneMap1";
                }
            }
            if (SceneManager.sceneName == "SceneSkill")// 技能列表
            {

                if (InputSystem.Instance.IsKeyPressed(ConsoleKey.I))// 技能列表切回主场景
                {
                    SkillSystem.IsOpen = !SkillSystem.IsOpen;
                    SceneManager.sceneName = "SceneMap1";
                }
            }
            if (SceneManager.sceneName == "SceneCombat")// 战斗
            {
                cs.Input();
            }
            if (SceneManager.sceneName == "SceneDialog")// 对话
            {
                ds.Input();
            }
        }

        public void Update()
        {
            if (SceneManager.sceneName == "SceneMap1")// 主场景
            {
                hero.Update();
                for (int i = 0; i < slime.Count; i++)
                {
                    ((EnemySlime)slime[i]).Update();
                }
                for (int i = 0; i < goblin.Count; i++)
                {
                    ((EnemyGoblin)goblin[i]).Update();
                }
                
            }
            if (SceneManager.sceneName == "SceneCombat")// 战斗
            {
                cs.Update();
            }
            if (SceneManager.sceneName == "SceneDialog")// 对话
            {
                ds.Update();
            }
        }

        public void FixUpdate()
        {
            hero.FixUpdate();
        }

        public GameObject? GetGameObject(List<GameObject> list, Pos pos)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Pos == pos)
                {
                    return list[i];
                }
            }
            return null;
        }

        void PlayerInteracion(Pos pos)
        {
            GameObject? fk = GetGameObject(fKing, pos);
            if (fk != null)
            {
                // 交互逻辑
                ds.StartDialog(((Npc)fk).DialogId);
                SceneManager.sceneName = "SceneDialog";
                dsTarget = "fk";
                Character.IsDialoging = true;

                //对话选项逻辑
                ds.selection1 += () =>
                {
                    InfoSystem.Instance.AddInfo($"国王留下了一把钥匙",ConsoleColor.Yellow);
                    fKing.Remove(fk);
                    map.SetType(pos, Map.Type.Map1Key0);
                };
            }
            GameObject? m = GetGameObject(maO, pos);
            if (m != null)
            {
                // 交互逻辑
                ds.StartDialog(((Npc)m).DialogId);
                SceneManager.sceneName = "SceneDialog";
                dsTarget = "m";
                Character.IsDialoging = true;

                //对话选项逻辑
                ds.selection1 += () =>
                {
                    EventSystem.Instance.InvokeEvent("魔王战");
                    maO.Remove(m);
                    map.SetType(pos, Map.Type.None);
                };
            }
            GameObject? rk = GetGameObject(rKing, pos);
            if (rk != null)
            {
                // 交互逻辑
                ds.StartDialog(((Npc)rk).DialogId);
                SceneManager.sceneName = "SceneDialog";
                dsTarget = "rk";
                Character.IsDialoging = true;

                //对话选项逻辑
                ds.selection1 += () =>
                {
                    InfoSystem.Instance.AddInfo("*获得技能书<奥术飞弹>*", ConsoleColor.Yellow);
                    EventSystem.Instance.InvokeEvent("奥术飞弹");
                    ((Npc)rk).DialogId = 26;
                    Map.rKingDialogId = 26;
                    isBoss = true;
                };
                ds.selection2 += () =>
                {
                    ((Npc)rk).DialogId = 22;
                    Map.rKingDialogId = 22;
                };
            }
            GameObject? n1 = GetGameObject(npc1, pos);
            if (n1 != null)
            {
                // 交互逻辑
                ds.StartDialog(((Npc)n1).DialogId);
                SceneManager.sceneName = "SceneDialog";
                dsTarget = "n1";
                Character.IsDialoging = true;

                ds.selection1 += () =>
                {
                    ((Npc)n1).DialogId = 204;
                    Map.npc1DialogId = 204;
                };
            }
            GameObject? n2 = GetGameObject(npc2, pos);
            if (n2 != null)
            {
                // 交互逻辑
                ds.StartDialog(((Npc)n2).DialogId);
                SceneManager.sceneName = "SceneDialog";
                dsTarget = "n2";
                Character.IsDialoging = true;

                
            }
            GameObject? p = GetGameObject(pastor, pos);
            if (p != null)
            {
                // 交互逻辑
                ds.StartDialog(((Npc)p).DialogId);
                SceneManager.sceneName = "SceneDialog";
                dsTarget = "p";
                Character.IsDialoging = true;

                ds.selection1 += () =>
                {
                    //改变id
                    ((Npc)p).DialogId = 244;
                    Map.pasterDialogId = 244;
                };
                ds.selection2 += () =>
                {
                    //治疗
                    hero.Hp = hero.MaxHp;
                    hero.Mp = hero.MaxMp;
                    InfoSystem.Instance.AddInfo("治疗完毕,血量已恢复", ConsoleColor.Yellow);
                };
            }
            
        }

        public void Control()
        {
            Utility.MyPrint("移动：   W     |  背包：O|交互：Enter     ", 0, 30);
            Utility.MyPrint("选择: S  A  D  |技能栏：I|返回: Esc          ", 0, 31);
        }

        public void Render()
        {
            hero.Render();
            if (SceneManager.sceneName == "SceneMap1")// 主场景
            {
                dia = 0;
                map.Render();
                Control();
                for (int i = 0; i < slime.Count; i++)
                {
                    ((EnemySlime)slime[i]).Render();
                }
                for (int i = 0; i < goblin.Count; i++)
                {
                    ((EnemyGoblin)goblin[i]).Render();
                }
                for (int i = 0; i < fKing.Count; i++)
                {
                    ((Npc)fKing[i]).Render();
                }
                for (int i = 0; i < rKing.Count; i++)
                {
                    ((Npc)rKing[i]).Render();
                }
                for (int i = 0; i < npc1.Count; i++)
                {
                    ((Npc)npc1[i]).Render();
                }
                for (int i = 0; i < npc2.Count; i++)
                {
                    ((Npc)npc2[i]).Render();
                }
                for (int i = 0; i < pastor.Count; i++)
                {
                    ((Npc)pastor[i]).Render();
                }
                for (int i = 0; i < maO.Count; i++)
                {
                    ((Npc)maO[i]).Render();
                }
            }
            if (SceneManager.sceneName == "SceneCombat")// 战斗
            {
                cs.Render();
                cs.map.Render();
            }
            if (SceneManager.sceneName == "SceneDialog")// 对话
            {
                if (dia < 1)
                {
                    Console.Clear();
                    dia++;
                }
                ds.Render();
            }
            else
            {
                InfoSystem.Instance.PrintInfo();
            }
        }
        public void End()
        {
            
        }

    }
}
